﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;

namespace eWallet.Data.Local.eWalletDataSetTableAdapters
{
	public partial class SecretsTagsTableAdapter
	{
		/// <summary>
		/// Creates a new instance of the table adapter and specifies the full file
		/// path to the SQL Server Compact Edition database to connect to.
		/// </summary>
		/// <param name="dbPath">The full file path to the database file.</param>
		public SecretsTagsTableAdapter(string dbPath)
		{
			this._connection = new SqlCeConnection(string.Format("Data Source={0}", dbPath));
		}



		public int Delete(Guid? tagId, Guid? secretId)
		{
			int i = 0;

			using (eWalletDataSet ds = new eWalletDataSet())
			{
				this.Fill(ds.SecretsTags, tagId, secretId);
				foreach (var row in ds.SecretsTags)
				{
					row.Delete();
				}

				i = this.Update(ds.SecretsTags);
			}
			
			return i;
		}

		public int Fill(eWalletDataSet.SecretsTagsDataTable table, Guid? tagId, Guid? secretId)
		{
			int i = 0;

			using (SqlCeCommand cmd = new SqlCeCommand())
			{
				cmd.Connection = this.Connection;
				List<string> wheres = new List<string>();
				if (tagId.HasValue)
				{
					wheres.Add("[TagId] = @TagId");
					cmd.Parameters.AddWithValue("@TagId", tagId.Value);
				}
				if (secretId.HasValue)
				{
					wheres.Add("[SecretId] = @SecretId");
					cmd.Parameters.AddWithValue("@SecretId", secretId.Value);
				}

				StringBuilder builder = new StringBuilder();
				builder.Append("SELECT * FROM [SecretsTags]");
				if (wheres.Count > 0)
				{
					builder.Append(" WHERE ");
					builder.Append(string.Join(" AND ", wheres.ToArray()));
				}

				cmd.CommandText = builder.ToString();

				using (SqlCeDataAdapter adr = new SqlCeDataAdapter(cmd))
				{
					i = adr.Fill(table);
				}
			}

			return i;
		}

		public eWalletDataSet.SecretsTagsDataTable GetData(Guid? tagId, Guid? secretId)
		{
			eWalletDataSet.SecretsTagsDataTable table = new eWalletDataSet.SecretsTagsDataTable();
			this.Fill(table, tagId, secretId);
			return table;
		}

	}
}
